package com.meixiang.activity.moments;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.adapter.moments.AccusationAdapter;
import com.meixiang.entity.moments.ReportList;
import com.meixiang.entity.moments.ReportListBean;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.tool.Tool;
import com.meixiang.util.AbJsonUtil;
import com.meixiang.view.swipetoloadlayout.base.OnRefreshListener;
import com.meixiang.view.swipetoloadlayout.base.SwipeToLoadLayout;

import org.json.JSONObject;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 作者：黑哥 on 2016/5/7 11:01
 * <p/>
 * 举报
 */
public class AccusationActivity extends BaseActivity implements OnRefreshListener{
    private Activity mActivity;
    @Bind(R.id.swipe_target)
    RecyclerView mRecyclerView;
    @Bind(R.id.refresh)
    SwipeToLoadLayout mRefresh;

    private AccusationAdapter mAdapter;

    private String  reportId;
    private String commentId;
    private  ReportListBean bean;

    private List<ReportList> mList;
    private boolean isRefresh = true;
    private boolean isSelect =false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_accusation);
        mActivity=AccusationActivity.this;
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        if(getIntent().getExtras()!=null){
            commentId=getIntent().getExtras().getString("CommentId");
        }
        setRightTitle("举报", "提交", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //当选中的时候才可以提交
                if(isSelect==true){
                    getReportPublish();
                }else{
                    Tool.showTextToast(mActivity, "请选择举报原因");
                }

            }
        });
    }

    @Override
    protected void initView() {


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mAdapter = new AccusationAdapter(mActivity);
        mRecyclerView.setAdapter(mAdapter);
//        mAdapter.setOnItemClickListener(new AccusationAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(View view, int position) {
//                //得到当前选中的id
//                isSelect=true;
//                reportId=mAdapter.getData().get(position).getReportId();
//            }
//        });
        mAdapter.setOnItemClickListener(new AccusationAdapter.onItemClickListener() {
            @Override
            public void onItemClick(View v, int position, ImageView mIvSelected) {
                //得到当前选中的id
                isSelect=true;
                reportId=mAdapter.getData().get(position).getReportId();
                mAdapter.selectedPosition=position;
                mAdapter.notifyDataSetChanged();


                //改变 按钮颜色


            }
        });
        /**检查RecyclerView是否滑动底部如果滑到*/
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (!ViewCompat.canScrollVertically(recyclerView, 1)) {
                        mRefresh.setLoadingMore(true);
                    }
                }
            }
        });
    }

    @Override
    protected void setData() {

        getAccusationData(true);


    }

    private void getAccusationData(final boolean isPull) {

        HttpParams params = new HttpParams();

        HttpUtils.post(Config.MOMENTS_REPORT_CAUSE, params, new HttpCallBack(mActivity) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
//                String json = null;
//                try {
//                    onCompleteRefresh();
//                    json = result.getString("reportList");
//                    mBean = new Gson().fromJson(json, new TypeToken<List<ReportListBean>>() {
//                    }.getType());
//                    mAdapter.addAll(mBean);
//
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }


                if (isPull){
                    mAdapter.clear();
                }
                bean= AbJsonUtil.fromJson(result.toString(),ReportListBean.class);
                mList=bean.getReportList();
                if (mList==null||mList.size()==0){
                    status.showNoData("暂无数据");
                }else{
                    status.removeView();
                    mAdapter.addAll(mList);
                }

            }

            @Override
            public void onError(String code, String message) {
                Tool.showTextToast(mActivity, message);
            }
        });



    }
    /**提交举报*/
    private void getReportPublish() {
        Log.e("TAG","请求参数reportId=="+reportId+"--commentId="+commentId);
        HttpParams params = new HttpParams();
        params.put("reportId",reportId);
        params.put("commentId",commentId);
        HttpUtils.post(Config.MOMENTS_REPORT_PUBLISH, params, new HttpCallBack(mActivity) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                Tool.showTextToast(mActivity, message);
//                finish();
            }

            @Override
            public void onError(String code, String message) {
                Tool.showTextToast(mActivity, message);
            }
        });



    }


    @Override
    public void onClick(View v) {

    }

    private void onCompleteRefresh() {
        if (mRefresh != null) {
            if (isRefresh == true) {
                mRefresh.setRefreshing(false);
            } else {
                mRefresh.setLoadingMore(false);
            }
        }
    }

    /**
     * 下拉刷新
     */
    @Override
    public void onRefresh() {
        getAccusationData(true);

    }


}
